var prefix = api + "/gen/tool/gen";
var suffix="?Authorization="+'Bearer ' + session.get('token')+"&TENANT-ID="+session.get('tenantId');

$(function() {
	var ds = datasourceList()
	renderStatisticsFilter('#datasource',ds)
	var options = {
		url: prefix + "/list",
		updateUrl: "/pages/tool/gen/edit",
		removeUrl: prefix + "/{id}",
		createTime: false,
		//sortName: "createTime",
		//sortOrder: "desc",
		//showExport: true,
		modalName: "生成配置",
		rememberSelected: true,
		uniqueId: "tableId",
		columns: [{
				field: 'state',
				checkbox: true
			},
			{
				field: 'tableId',
				title: '编号',
				visible: false
			},
			{
				title: "序号",
				formatter: function(value, row, index) {
					return $.table.serialNumber(index);
				}
			},
			{
				field: 'dsName',
				title: '数据源名称',
				sortable: true,
				formatter: function(value, row, index) {
					return $.table.tooltip(value);
				}
			},
			{
				field: 'tableName',
				title: '表名称',
				sortable: true,
				formatter: function(value, row, index) {
					return $.table.tooltip(value);
				}
			},
			{
				field: 'tableComment',
				title: '表描述',
				sortable: true,
				formatter: function(value, row, index) {
					return $.table.tooltip(value, 15);
				}
			},
			{
				field: 'className',
				title: '实体类名称',
				sortable: true
			},
			{
				field: 'createTime',
				title: '创建时间',
				sortable: true
			},
			{
				field: 'updateTime',
				title: '更新时间',
				sortable: true
			},
			{
				title: '操作',
				align: 'center',
				formatter: function(value, row, index) {
					var actions = [];
					actions.push('<a class="btn btn-info btn-xs" href="javascript:void(0)" onclick="preview(\'' + row.tableId +
						'\')"><i class="fa fa-search"></i>预览</a> ');
					actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="$.operate.editTab(\'' +
						row.tableId + '\')"><i class="fa fa-edit"></i>编辑</a> ');
					actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.tableId +
						'\')"><i class="fa fa-remove"></i>删除</a> ');
					actions.push('<a class="btn btn-primary btn-xs" href="javascript:void(0)" onclick="genCode(\'' + row.tableName +
						'\')"><i class="fa fa-bug"></i>生成代码</a> ');
					return actions.join('');
				}
			}
		]
	};
	$.table.init(options);
});

function datasourceList() {
    var result = []
    $.ajax({
        url: api + '/gen/dsconf/list',
        method: 'get',
        async: false
    }).done(function (res) {
        result = res.data
    })
    return result
}

function renderStatisticsFilter(obj,ds) {
    ds.forEach(function (item) {
        $(obj).append('<option value="' + item.name + '">' + item.name + '</option>')
    })
}
// 预览代码
function preview(tableId) {
	var preViewUrl = prefix + "/preview/" + tableId;
	$.modal.loading("正在加载数据，请稍后...");
	$.get(preViewUrl, function(result) {
		if (result.code == web_status.SUCCESS) {
			var items = [];
			$.each(result.data, function(index, value) {
				value = value.replace(/</g, "&lt;");
				value = value.replace(/>/g, "&gt;");
				var templateName = index.substring(index.lastIndexOf("/") + 1, index.length).replace(/\.vm/g, "");
				//if(!$.common.equals("sql", templateName) && !$.common.equals("tree.html", templateName)){
				items.push({
					title: templateName,
					content: "<pre class=\"layui-code\">" + value + "</pre>"
				})
				//}
			});
			top.layer.tab({
				area: ['90%', '90%'],
				shadeClose: true,
				tab: items
			});
		} else {
			$.modal.alertError(result.msg);
		}
		$.modal.closeLoading();
	});
}

// 生成代码
function genCode(tableName) {
	$.modal.confirm("确定要生成" + tableName + "表代码吗？", function() {
		location.href = prefix + "/genCode/" + tableName+suffix;
		layer.msg('执行成功,正在生成代码请稍后…', {
			icon: 1
		});
	})
}

//批量生成代码
function batchGenCode() {
	var rows = $.table.selectColumns("tableName");
	if (rows.length == 0) {
		$.modal.alertWarning("请选择要生成的数据");
		return;
	}
	$.modal.confirm("确认要生成选中的" + rows.length + "条数据吗?", function() {
		location.href = prefix + "/batchGenCode"+suffix+"&tables=" + rows;
		layer.msg('执行成功,正在生成代码请稍后…', {
			icon: 1
		});
	});
}

// 导入表结构
function importTable() {
	var importTableUrl = "/pages/tool/gen/importTable";
	$.modal.open("导入表结构", importTableUrl);
}
function editTable(tableId) {
	var importTableUrl = "/pages/tool/gen/edit?tableId="+tableId;
	$.modal.open("修改生成配置", importTableUrl);
}